var ctx=window.daijia.ctx;
var newTotalElements_zhuan=0;
var off_set_zhuan=null;
$(document).ready(function(){
	
	var newDate = new Date();
	var startDate = new Date();
	startDate.setDate(startDate.getDate()-30);
	var startTimeStr=startDate.getFullYear()+"-"+((startDate.getMonth()+1)>9?(startDate.getMonth()+1):'0' + (startDate.getMonth()+1))+"-"+(startDate.getDate()>9?startDate.getDate():'0' + startDate.getDate())+" "+"00:00";
	var endTimeStr=newDate.getFullYear()+"-"+((newDate.getMonth()+1)>9?(newDate.getMonth()+1):'0' + (newDate.getMonth()+1))+"-"+(newDate.getDate()>9?newDate.getDate():'0' + newDate.getDate())+" "+"00:00";
	$("#startTime3").val(startTimeStr);
	$("#endTime3").val(endTimeStr);
	
	$("#startTime3").datetimepicker({
        format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	$("#endTime3").datetimepicker({
		format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	
	initPagedata3(getTotalElements3(),true);
	
	//点击搜索
	$("#search_button3").click(function(){
		if($("#startTime3").val()=="" || $("#endTime3").val()==""){
			$.scojs_message("日期不能为空", $.scojs_message.TYPE_ERROR);
			return false;
		}
		var DATE_FORMAT = /^[0-9]{4}-[0-1]{1}[0-9]{1}-[0-3]{1}[0-9]{1} [0-2]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$/;
		if(!DATE_FORMAT.test($("#startTime3").val()) || !DATE_FORMAT.test($("#endTime3").val())){
		   $.scojs_message("您输入的日期格式有误，正确格式应为：2012-01-01 08:30", $.scojs_message.TYPE_ERROR);
		   return false;
		}
		if($("#startTime3").val()>$("#endTime3").val()){
			 $.scojs_message("开始日期不能大于结束日期", $.scojs_message.TYPE_ERROR);
			 return false;
		}
		
		$("#dateRangeText3").text($("#startTime3").val()+" 至 "+$("#endTime3").val());
		$("#dateRangeButton3").parent().find(".dropdown-calendar").hide();
		
		 newTotalElements_zhuan=getTotalElements3();
		initPagedata3(newTotalElements_zhuan);
			$("#zhuanpage").find("li a").html("共 "+newTotalElements_zhuan+" 条数据");
	});
		

	//导出
	$("#zhuan_export_button").click(function(){
		
		window.location=ctx+"/zhuanche/exportZhuanMemberOrder?pager.offset="+off_set_zhuan+"&startTime="+$("#startTime3").val()+
						"&endTime="+$("#endTime3").val()+(isMain?"&companyId="+$("#companyId3").val():"")+"&gradeId="+$("#grade_zhuan").val()+
						"&type="+$("#order_zhuan").val();
	});
});

function loadData3(page){
	
	if($('#loadblacklayer3').length==0){
		var dw = $("#zhuan").width();
		var dh = $("#zhuan").height();
		var backlayer ="<div id='loadblacklayer3'><div style='margin-top:"+(dh/2)+"px;margin-left:"+(dw/2)+"px;'><img src='"+ctx+"/assets/css/images/loading.gif' /></div></div>"
		$("#zhuan tbody").append(backlayer);
		$('#loadblacklayer3').css({"z-index":"9","background":"#000","opacity":"0.5","position":'absolute',"top":0,"width":dw,"height":dh});
	}
	
	off_set_zhuan=(page-1)*20;
	var newcontent_zhuan = '';
	
	
	$.ajax({
        url : ctx+'/zhuanche/zhuanMemberOrderData',
        type:'GET',
        async:true,
        dataType : 'json',
        data : {
        	"pager.offset":off_set_zhuan,
        	"startTime":$("#startTime3").val(),
        	"endTime":$("#endTime3").val(),
        	"companyId":$("#companyId3").val(),
        	"gradeId":$("#grade_zhuan").val(),
        	"type":$("#order_zhuan").val()
        },
        success : function(result){
        	if(result.totalElements>0){
        		newcontent_zhuan+="";
        		$(result.content).each(function(index,data){
        			
					if(data.name.indexOf("总计")!=-1){
						newcontent_zhuan+="<tr class='content warning grid-data tr-hui'>"+
					    "<td>"+data.name+"</br>"+data.phone+"</td>"+
						"<td>"+data.companyName+"</td>"+
						"<td><span style='color:#ff950b;font-weight:700;'>"+data.finishNums+"</span></td>"+
						"<td><span style='color:#ff950b;font-weight:700;'>"+data.money.toFixed(1)+"</span></td>"+
						"</tr>";
					   }else{
						   newcontent_zhuan+="<tr class='content grid-data tr-hui'>"+
						    "<td>"+data.name+"</br>"+data.phone+"</td>"+
							"<td>"+data.companyName+"</td>"+
							"<td><span style='color:#ff950b;font-weight:700;'>"+data.finishNums+"</span></td>"+
							"<td><span style='color:#ff950b;font-weight:700;'>"+data.money.toFixed(1)+"</span></td>"+
							"</tr>";
					   }
        		});
        		 newcontent_zhuan+="</tbody>";
        	}
        	$('#loadblacklayer3').remove();
        	$("#zhuan tbody").remove();
        	$("#zhuan").append(newcontent_zhuan);
        	$("#zhuan tbody").show(300);
    		newTotalElements_zhuan=result.totalElements;
    		$("#zhuanpage").find("li a").html("共 "+newTotalElements_zhuan+" 条数据");
        }
	});
	
	return newTotalElements_zhuan;
}

//分页初始化
function initPagedata3(total){
	$("#zhuan_data").paging(total, {
		format: '[< ncnnn >]',
		perpage: 20,
		onSelect: function (page) {
			loadData3(page);
	    	return false;
		},
		onFormat: function (type) {
			switch (type) {
			case 'block': // n and c
				if (!this.active)
					return '';
				else if (this.value != this.page)
					return '<li><a href="#">' + this.value + '</a></li>';
				return '<li class="active"><a href="#">' + this.value + '</a></li>';
			case 'next': // >
				return '<li><a href="#">&rsaquo;</a><li>';
			case 'prev': // <
					return '<li><a href="#">&lsaquo;</a><li>';
			case 'first': // [
					return '<li><a href="#">&laquo;</a><li>';
			case 'last': // ]
					return '<li><a href="#">&raquo;</a></li>';
			}
		}
	});
}

function getTotalElements3(){
	
	$.ajax({
        url : ctx+'/zhuanche/zhuanMemberOrderData',
        type:'GET',
        async:false,
        dataType : 'json',
        data :  {
        	"startTime":$("#startTime3").val(),
        	"endTime":$("#endTime3").val(),
        	"companyId":$("#companyId3").val(),
        	"gradeId":$("#grade_zhuan").val(),
        	"type":$("#order_zhuan").val()
        },
        success : function(result){
        	newTotalElements_zhuan=result.totalElements;
        }
	});
	
	return newTotalElements_zhuan;
	}
	



